package com.hb.trade.server.project.calibration.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.enums.DeployStatusEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.calibration.dto.BidWinResultDTO;
import com.hb.core.pojo.project.calibration.dto.PortalBidWinDTO;
import com.hb.core.pojo.project.calibration.entity.BidWinResult;
import com.hb.core.pojo.project.calibration.mapper.BidWinResultMapper;
import com.hb.trade.server.project.calibration.service.BidWinResultService;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 中标结果公示/公告表 服务实现类
 * </p>
 *
 * @author zhongdl
 * @since 2021-12-18
 */
@Service
public class BidWinResultServiceImpl extends ServiceImpl<BidWinResultMapper, BidWinResult> implements BidWinResultService {

    /**
     *
     * @Description: 更新中标公示状态
     * @Author: zhongdl
     * @Date:2021/12/18 17:09
     * @param id: 中标公示id
     * @param deployStatusEnum: 中标公示状态
     * @return: {@link Boolean }
     **/
    @Override
    public boolean updateDeployStatusById(Long id, DeployStatusEnum deployStatusEnum) {
        boolean flag = true;
        if (id == null) {
            return flag;
        }
        UpdateWrapper<BidWinResult> update = new UpdateWrapper<>();
        update.eq("id", id);
        update.set("deploy_status", deployStatusEnum.getCode());
        update.set("deploy_time", new Date());
        return this.update(update);
    }

    /**
     *
     * @Description: 分页查询中标结果公告的
     * @Author: zhongdl
     * @Date:2021/12/20 9:36
     * @param bidWinResultDTO:
     * @return: {@link PageResult< BidWinResultDTO> }
     **/
    @Override
    public PageResult<BidWinResultDTO> pageBidWinResult(BidWinResultDTO bidWinResultDTO) {
        Page<BidWinResult> pages = bidWinResultDTO.pages();
        QueryWrapper<BidWinResult> wrapper = BidWinResult.wrapper();
        wrapper.eq("project_id", bidWinResultDTO.getProjectId()).orderByDesc("create_time");
        page(pages, wrapper);
        return PageResult.get(pages).transferTo(BidWinResultDTO.class);
    }

    /**
     *
     * @Description: 判断中标结果公告是否已发布
     * @Author: zhongdl
     * @Date:2021/12/20 16:34
     * @param id:
     * @return: {@link Boolean }
     **/
    @Override
    public boolean releaseFlagById(Long id) {
        if (id == null) {
            return false;
        }
        BidWinResult bidWinResult = getById(id);
        if (bidWinResult == null) {
            return false;
        }
        return Enums.equals(DeployStatusEnum.DEPLOYED, bidWinResult.getDeployStatus());
    }

    /**
     *
     * @Description: 根据项目id查询 未新建中标结果公告的标段 并且已经评标结果状态的
     * @Author: zhongdl
     * @Date:2022/1/5 14:07
     * @param bidWinResult:
     * @return: {@link List< BidWinResultDTO> }
     **/
    @Override
    public PageResult<BidWinResultDTO> pageNotSelectBidSection(BidWinResultDTO bidWinResult) {
        Page<BidWinResultDTO> pages = bidWinResult.pages();
        //TODO 暂时限制为已经开标的标段 就能发布中标结果公示  以后开发完评标系统之后在传标段状态
        Page<BidWinResultDTO> page = baseMapper.pageNotSelectBidSection(pages, bidWinResult.getProjectId());
        return PageResult.get(page);
    }

    /**
     *
     * @Description: 根据标段id分页查询已经发布的中标结果公告列表
     * @Author: zhongdl
     * @Date:2022/1/5 16:30
     * @param bidWinResultDTO:
     * @return: {@link PageResult< BidWinResultDTO> }
     **/
    @Override
    public PageResult<BidWinResultDTO> pageBidWinResultBySectionId(BidWinResultDTO bidWinResultDTO) {
        Page<BidWinResultDTO> pages = bidWinResultDTO.pages();
        baseMapper.pageBidWinResultBySectionId(pages, bidWinResultDTO.getSectionId());
        return PageResult.get(pages);
    }

    /**
     *
     * @Description: 判断标段中标结果公告是否已发布
     * @Author: zhongdl
     * @Date:2021/12/20 16:34
     * @param sectionId:
     * @return: {@link Boolean }
     **/
    @Override
    public boolean releaseFlagBySectionId(Long sectionId) {
        if (sectionId == null) {
            return false;
        }
        return CollUtil.isNotEmpty(baseMapper.listBidWinResultBySectionId(sectionId));
    }

    /**
     *门户网站查询对应的结果公示
     * @param portalBidWinDTO
     * @return
     * @Author: LJS
     * @Date:2022-2-18 11:34:39
     */
    @Override
    public PageResult<PortalBidWinDTO> pageBidWinPortal(PortalBidWinDTO portalBidWinDTO) {
        Page<PortalBidWinDTO> portalBidWinDTOPage = baseMapper.pageBidWinPortal(portalBidWinDTO.pages(), portalBidWinDTO);
        return PageResult.get(portalBidWinDTOPage);
    }

    /**
     *门户网站查询对应的结果公示
     * @param portalBidWinDTO
     * @return
     * @Author: LJS
     * @Date:2022-2-18 11:34:39
     */
    @Override
    public PortalBidWinDTO detailBidWinPortal(PortalBidWinDTO portalBidWinDTO) {
        PortalBidWinDTO bidWinDTO = baseMapper.detailBidWinPortal(portalBidWinDTO);
        return bidWinDTO;
    }

    /**
     *
     * @Description:查询所需要推送的中标结果公告
     * @Author: zhongdl
     * @Date:2022/2/24 14:26
     * @return: {@link List< BidWinResultDTO> }
     **/
    @Override
    public List<BidWinResultDTO> listPushBidWinResult() {
        return baseMapper.listPushBidWinResult();
    }
}
